// pages/weixiu/weixiu.js
const {weixiuwancheng} =require('../../util/api')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imglist:[],
    imgUrls:"",
    remarks:'',
    orderId:''
  },

  textInput(e){
    
    this.setData({
      remarks:e.detail.value
    })
  },

  add(){
    if(this.data.imglist.length >= 5)
    {
      wx.showToast({
        title: '最大上传5张照片',
        icon:'none'
      })
      return
    }
    let that=this
    let imgs = this.data.imglist
    wx.chooseMedia({
      count:1,
      mediaType:['image'],
      sourceType:['album', 'camera'],
      success(res) {
        imgs.push(res.tempFiles[0])
        that.setData({
          imglist:imgs
        })
        wx.uploadFile({
          filePath: res.tempFiles[0].tempFilePath,
          name: 'file',
          url: 'https://anjingbao.anjingbao.net/prod-api/api/common/appUpload',
          success (res){
            let jsonS = JSON.parse(res.data)
            that.setData({
              imgUrls:that.data.imgUrls + jsonS.fileName + ","
            })
            
            console.log('img', that.data.imgUrls)
            //do something
          }
        })
      }

    })

  },
  handtap(){
    if(this.data.remarks.length == 0)
    {
      wx.showToast({
        title: '请输入描述内容',
        icon:'none'
      })
    }
    else if(this.data.imglist.length == 0)
    {
      wx.showToast({
        title: '请至少上传一张图片',
        icon:'none'
      })
    }
    else
    {
      let params = {
        userId:wx.getStorageSync('userId'),
        workId:this.data.id,
        imgs:this.data.imgUrls.substring(0, this.data.imgUrls.length - 1),
        remarks:this.data.remarks,
      }
      console.log(params)
      weixiuwancheng(params).then(res=>{
        console.log(2222222,res);
        if (res.code ===200){
          wx.showModal({
            title:'提示',
            content:'维修完成',
            success (res) {
             if (res.confirm) {
               wx.navigateBack({
                 delta: 1,
               })
              }
           }
          })
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log('op', options)
    this.setData({
      id:options.id
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})